package edu.purdue.touch.util;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import edu.umd.cs.piccolo.PCanvas;

public class LogParser implements ActionListener{
	/**
	 * 
	 */
	private JFrame frame;
	private static final long serialVersionUID = 1L;
	private PCanvas canvas;
	private int heatMapWidth = 640;
	private int heatMapHeight = 480;
//	private String location;

	public LogParser(String title) {
		 frame = new JFrame(title);
		 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		 frame.setPreferredSize(new Dimension(1024,768));
		 
		 canvas = new PCanvas();
		 canvas.setPreferredSize(new Dimension(heatMapWidth,heatMapHeight));
		 
		 JPanel topPanel = new JPanel();		 
		 JButton button = new JButton("Take SVG screen shot");
		 button.addActionListener(this);	 
		 topPanel.add(button,BorderLayout.CENTER);
		 button = new JButton("Clear");
		 button.addActionListener(this);
		 topPanel.add(button,BorderLayout.CENTER);
		
		 
		 frame.getContentPane().add(topPanel, BorderLayout.NORTH);
		 frame.getContentPane().add(canvas,BorderLayout.CENTER);
		 
		 buildMenu();		 	 
		 
		 frame.pack();
	}
	
	public void buildMenu()
	{
		JMenu menu;
		JMenuBar menuBar;
		JMenuItem menuItem;
		
		menuBar = new JMenuBar();
		
		menu = new JMenu("File");
		menu.setMnemonic(KeyEvent.VK_F);
		
		menuItem = new JMenuItem("Open...");
		menuItem.addActionListener(this);
		menu.add(menuItem);
		
		menuBar.add(menu);
		
		frame.setJMenuBar(menuBar);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("Open...")) {
			JFileChooser fc = new JFileChooser(".");
			int result = fc.showOpenDialog(frame);
			
			if (result == JFileChooser.APPROVE_OPTION) {
//				Object[] places = {"Purdue", "Manitoba","Local"};
//				location = (String)JOptionPane.showInputDialog(
//				                    frame,
//				                    "Where are you connecting from?",
//				                    "Client Setup", JOptionPane.PLAIN_MESSAGE,
//				                    null,
//				                    places,
//				                    "Purdue");
//				double[][] heatmapData = generateData2D(fc.getSelectedFile());
//				heatMap = new HeatMap(heatmapData, true, Gradient.GRADIENT_HEAT);
//				frame.getContentPane().add(heatMap);
//				addTraces(heatmapData);								
			}
		}
		else if(e.getActionCommand().equals("Take SVG screen shot")) {
			//SVGScreenshotTool.sayCheese(canvas, "test.svg");
			//System.err.println("Captured");
		}
		else if(e.getActionCommand().equals("Clear"))
		{
			canvas.getLayer().removeAllChildren();
		}
	}
	
	/*
	private void addTraces(double[][] data) {
		float blob_size = 10;
		
		for (int i = 0 ; i < heatMapWidth ; i++)
		{
			for(int j = 0 ; j < heatMapHeight ; j ++)
			{
				PPath blob = PPath.createEllipse((float)i, (float)j, blob_size, blob_size);
				blob.setStrokePaint(new Color(0xFFFFFF));
				blob.setTransparency(0.1f);
				
				canvas.getLayer().addChild(blob);
			}
		}		
		
		canvas.repaint();
	}
	
	private double[][] generateData2D( File file )
	{
		
		double[][] result = new double[heatMapWidth][heatMapHeight];
		for (int i = 0 ; i < heatMapWidth ; i++)
		{
			for(int j = 0 ; j < heatMapHeight ; j ++)
			{
				result[i][j]=0;
			}
		}
		
		try{
		    FileInputStream fstream = new FileInputStream(file);
		    // Get the object of DataInputStream
		    DataInputStream in = new DataInputStream(fstream);
		    BufferedReader br = new BufferedReader(new InputStreamReader(in));
		    String strLine;
		    String[] element;
		    while ((strLine = br.readLine()) != null){
		    	element = strLine.split(",");
		    	if(element.length == 3)
		    	{
		    		int x = (int)(Double.parseDouble(element[1]) * heatMapWidth);
		    		int y = (int)(Double.parseDouble(element[2]) * heatMapHeight);
		    		
		    		
		    		if(x!=0&&y!=0)
		    		{	
		    			Color blobColor = new Color(0xFFFFFF);
		    			
		    			if(location.equals("Purdue"))
		    			{
		    				blobColor = new Color(0xD8B365);
		    			}
		    			else if(location.equals("Manitoba"))
		    			{
		    				blobColor = new Color(0x5AB4AC);
		    			}
		    			
		    			PPath blob = PPath.createEllipse((float)x, (float)y, 20, 20);
		    			blob.setPaint(blobColor);
		    			blob.setStrokePaint(blobColor);
						blob.setTransparency(0.1f);
						
						canvas.getLayer().addChild(blob);
		    		}
		    		
		    		
		    		
		    		if(x>=1) x--;
		    		if(y>=1) y--;
		    		
		    		result[x][y]++;		    		
		    	}
		    }
		    //Close the input stream
		    in.close();
		    }catch (Exception e){//Catch exception if any
		      System.err.println("Error: " + e.getMessage());
		    }
		
		
		return result;
	}
	*/
	
	public void run() {
		frame.setVisible(true);
	}
	
	public static void main(String[] args) {
		LogParser logParser = new LogParser("Log Parser");
		logParser.run();
	}
}
